class Boom extends BaseComp{
    private img_dot_1:eui.Image;
    private img_dot_2:eui.Image;
    private img_light_0:eui.Image;
    private img_light_1:eui.Image;
    private img_light_2:eui.Image;
    private img_light_3:eui.Image;
    private img_light_4:eui.Image;
    private img_light_5:eui.Image;
    private img_dragon:eui.Image;
    private img_boom:eui.Image;

    private timer:egret.Timer;
    private curIndex:number = 0;
    private img_fire:eui.Image;

    private speedY;
    private speedX;
    private rotationFlag;
    private a;//加速度

    public static bombList:Boom[] = [];

    constructor()
    {
        super('BoomView');
    }

    public init()
    {
        this.timer = new egret.Timer(100);
        EffectUtil.instance().scaleEffect(this.img_fire);
    }

    public born_1()
    {
        this.visible = true;
        this.a = 0.3;
        this.speedY = 20 + Math.random()*6;
        this.speedX = Math.random() > 0.5 ? 1 : -1;
        this.rotation = 45 - 90 * Math.random();
        this.rotationFlag = this.rotation > 0 ? 1 : -1;
    }

    public born_2(flag)
    {
        this.visible = true;
        this.a = 0.3;
        this.speedY = 7 + Math.random()*6;
        this.speedX = (2 + (Math.random() * 3 + 3)) * flag;
        this.rotation = 45 - 90 * Math.random();
        this.rotationFlag = this.rotation > 0 ? 1 : -1;
    }

    public action_1()
    {
        this.speedY -= this.a;
        this.x += this.speedX ;
        this.y -= this.speedY ;
        this.rotation += this.rotationFlag;
    }

    public boom()
    {
        this.timer.start();
    }

    public cutAllFruit()
    {
        let logic:AwardCustomsUI = UIManager.instance().getLogicByName(UIConst.AWARDCUSTOMS_UI) as AwardCustomsUI;
        if(logic)
        {
            logic.cutAllFruit();
        }
        let logic1:EndLessUI = UIManager.instance().getLogicByName(UIConst.ENDLESS_UI) as EndLessUI;
        if(logic1)
        {
            logic1.cutAllFruit();
        }
    }

    public addEvent()
    {
        this.timer.addEventListener(egret.TimerEvent.TIMER, this.timeHandle, this);
    }

    public timeHandle()
    {
        if(this.curIndex < 9)
        {
            let img = this['img_light_'+this.curIndex] as eui.Image;
            this.curIndex ++;
            egret.Tween.get(img).to({
                scaleY:1
            }, 200);
            WeChatgGameManager.instance().vibrateShort();
        }
        else
        {
            this.timer.stop();
            egret.Tween.get(this.img_dot_2).to({
                scaleX:30,
                scaleY:30
            }, 800).call(()=>{
                this.curIndex = 0;
                for(let i=0;i<=8;i++)
                {
                    let img = this['img_light_'+i] as eui.Image;
                    img.scaleY = 0;
                }
                this.img_dot_2.scaleX = this.img_dot_2.scaleY = 0;
                this.img_dot_1.visible = false;
                this.img_boom.visible = false;
                this.img_fire.visible = false;
                this.cutAllFruit();
            })
        }
    }

    public removeEvent()
    {
        this.timer.removeEventListener(egret.TimerEvent.TIMER, this.timeHandle, this);
    }

    public clear()
    {
        super.clear();
        EffectUtil.instance().StopEffect(this.img_fire);
        this.timer = null;
        this.curIndex = null;

        this.speedY = null;
        this.speedX = null;
        this.rotationFlag = null;
        this.a = null;
    }

    public reset()
    {
        this.rotation = 0;
        this.alpha = 1;
        this.x = -100;
        Boom.bombList.push(this);
    }

    public static getBomb():Boom
    {
        let bomb ;
        if(Boom.bombList.length > 0)
        {
            bomb = Boom.bombList.shift();
        }
        else
        {
            bomb = new Boom();
        }
        return bomb;
    }

    public static clearPool()
    {
        let list = Boom.bombList;
        if(list.length > 0)
        {
            for(let v of list)
            {
                v.remove();
                v = null;
            }
        }
        Boom.bombList = [];
    }
}

window['Boom'] = Boom;